package cst.a703xmq.zyfypt_703xmq.Model;


import android.content.SharedPreferences;

import java.util.List;

import cst.a703xmq.zyfypt_703xmq.Bean.ArticleBean;
import cst.a703xmq.zyfypt_703xmq.Bean.ProjectBean;
import cst.a703xmq.zyfypt_703xmq.Bean.SVideoBean;
import cst.a703xmq.zyfypt_703xmq.Bean.TcaseBean;
import cst.a703xmq.zyfypt_703xmq.Bean.TwareBean;
import cst.a703xmq.zyfypt_703xmq.Bean.LoginBean;
import cst.a703xmq.zyfypt_703xmq.Bean.VideoBean;
import cst.a703xmq.zyfypt_703xmq.Service.ResService;
import cst.a703xmq.zyfypt_703xmq.Interface.XmqIface;
import cst.a703xmq.zyfypt_703xmq.Interface.XmqListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by bboy knowns on 2017/5/8.
 */

public class ResModel<T> implements XmqIface {
    private Retrofit retrofit;
    private ResService service;
    private static final String BASE_URL="http://amicool.neusoft.edu.cn/";
    private  String SESSION_ID ;
    private SharedPreferences sp;

    public ResModel(String type,String s_id) {
        if(type.equals("gson"))
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);
        SESSION_ID = s_id;
    }

    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else{
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);
        SESSION_ID = "tlb40fscfkka9q4eaatkvtajk2";
    }


    @Override
    public void login(String u_name, String u_pw, final XmqListener listener) {
        Call<LoginBean> call = service.login(u_name,u_pw);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response)
            {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t)
            {
                listener.onFail(t.toString());
            }
        });
    }

   @Override
   public void reg(String u, String p, String t, int roleid, final String realname, String email, final XmqListener listener) {
       Call<String> call = service.reg(u,p,t,roleid,realname,email);
       call.enqueue((new Callback<String>() {
           @Override
           public void onResponse(Call<String> call, Response<String> response) {
               listener.onResponse(response.body());
           }

           @Override
           public void onFailure(Call<String> call, Throwable t) {
               listener.onFail(t.toString());
           }
       }));
   }

    @Override
    public void getList(String mod, int page, final XmqListener listener) {
        if(mod == "video")
        {
            Call<List<VideoBean>> call = service.getVideoList(mod, page, SESSION_ID);
            call.enqueue(new Callback<List<VideoBean>>() {
                @Override
                public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                    if (response.isSuccessful() && response.body() != null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("vider_error");
                }
                @Override
                public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
        else if(mod == "article")
        {
            Call<List<ArticleBean>> call_a =
                    service.getArticleList(mod, page, SESSION_ID);
            call_a.enqueue(new Callback<List<ArticleBean>>() {
                @Override
                public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                    if (response.isSuccessful() && response.body() != null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("article_error");
                }
                @Override
                public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
        else if(mod == "tware")
        {
            Call<List<TwareBean>> call_c = service.getTwareList(mod, page, SESSION_ID);
            call_c.enqueue(new Callback<List<TwareBean>>() {
                @Override
                public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                    if (response.isSuccessful() && response.body() != null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("tware_error");
                }
                @Override
                public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
       else if(mod == "svideo")
        {
            Call<List<SVideoBean>> call_s = service.getSVideoList("video", page, SESSION_ID);
            call_s.enqueue(new Callback<List<SVideoBean>>() {
                @Override
                public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                    if (response.isSuccessful() && response.body() != null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("svider_error");
                }
                @Override
                public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
        else if(mod == "tcase")
        {
            Call<List<TcaseBean>> call_ca =
                    service.getTcaseList(mod, page, SESSION_ID);
            call_ca.enqueue(new Callback<List<TcaseBean>>() {
                @Override
                public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                    if (response.isSuccessful() && response.body() != null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("case_error");
                }
                @Override
                public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
        else if(mod == "project")
        {
            Call<List<ProjectBean>> call_p =
                    service.getProjectList(mod, page,SESSION_ID);
            call_p.enqueue(new Callback<List<ProjectBean>>() {
                @Override
                public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                    if(response.isSuccessful()&&response.body()!=null)
                        listener.onResponse(response.body());
                    else
                        listener.onFail("proj_c_error");
                }
                @Override
                public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }
    }
}
